class Vote < ActiveRecord::Base
  belongs_to :user
  belongs_to :posts

  def self.calculate_score( post_id )
    votes = Vote.find_all_by_post_id( post_id )
    vote_total = 0
    votes.each do |v|
      vote_total += v.value.to_i
    end
    return vote_total
  end

  def self.vote( user_id, post_id, value )
    vote = Vote.find_by_user_id_and_post_id( user_id, post_id )
    if vote.nil?
      vote = Vote.new
      vote.user_id = user_id
      vote.post_id = post_id
      vote.value = value
    else
      vote.value = value
    end
    vote.save
  end

end
